<?php
class admin_common extends CI_Controller {
	protected $_page_name = "admin_common";
	protected $_page_id = 'ul_postcontainer';	
	protected $_base_url = 'ul_postcontainer';	
	protected $data;
	
	function  __construct()  {
		$this->_initdata();
	}
	
	function _initdata()
	{
		parent::__construct();
		session_start();
		$this->load->model('Adminmodel');
		$this->load->helper('url');
	}		
	
	function index() {
		$this->show($this->data);
	}
	//Login
	function login()
	{
		$sCur_Page = "admin_login";
		$this->load->model('Adminmodel');
		$aUser_Details=array();
		$sUsername = $_POST['txt_username'];
		$sPassword = $_POST['txt_password'];
		list($iNum_Post,$aUser_Details)=$this->Adminmodel->_get_user_details($sUsername);
		if (!empty($aUser_Details)>0)
		{
			if ($sPassword != '' && md5($sPassword) != $aUser_Details[0]->password) {
				$_SESSION['msg'] = "Tên đăng nhập / Mật khẩu không đúng";
				$this->load->helper('url');
				redirect($sCur_Page, 'location', 301);						
			}
			$this->Adminmodel->_set_session(SESSION_ADMINTIMEOUT,time());
			$this->Adminmodel->_set_session(SESSION_ADMINUSERNAME,$sUsername);
			$this->Adminmodel->_set_session(SESSION_ADMINUSERID,$aUser_Details[0]->user_id);
		}
		else
		{
			$_SESSION['msg'] = "Tên đăng nhập / Mật khẩu không đúng";
			$this->load->helper('url');
			redirect($sCur_Page, 'location', 301);					
		}		
		$this->load->helper('url');
		redirect($this->data['base_url']."admin_home", 'location', 301);			
	}
	
	function logout()
	{
		$this->Adminmodel->_remove_session(SESSION_ADMINUSERNAME);
		$this->Adminmodel->_remove_session(SESSION_ADMINUSERID);
		$this->_check_admin_login();
	}
	
	function _check_admin_login()
	{
		if ($this->Adminmodel->_check_login(SESSION_ADMINUSERNAME) == false)
		{
			$base_url=$this->config->item('base_url');
			redirect($base_url.'admin_login', 'location', 301);
		}		
	}		
}
?>